/*
 * @Author: Drc
 * @Date: 2020-06-16 16:11:38
 * @LastEditors: Drc
 * @LastEditTime: 2021-09-30 17:12:11
 */

import {Platform, Dimensions, NativeModules} from 'react-native'
import AsyncStorage from '@react-native-community/async-storage'
import ExtraDimensions from 'react-native-extra-dimensions-android'

const DESIGN_WIDTH = 375
const DESIGN_HEIGHT = 812

const {width, height} = Dimensions.get('window')
const {StatusBarManager} = NativeModules

/**
 * 根据设计稿与真机比例放大缩小
 */
export const px2dp = (px: number) => {
  return px * (width / DESIGN_WIDTH)
}

class System {
  static width = width
  static height = Platform.OS === 'ios' ? height : ExtraDimensions.get('REAL_WINDOW_HEIGHT')
  static contentHeight =
    Platform.OS === 'ios'
      ? height
      : ExtraDimensions.get('REAL_WINDOW_HEIGHT') - ExtraDimensions.get('SOFT_MENU_BAR_HEIGHT')
  static menuHeight = Platform.OS === 'ios' ? 0 : ExtraDimensions.get('SOFT_MENU_BAR_HEIGHT')
  static headerHeight = 44
  static designHeight = DESIGN_HEIGHT
  static designWidth = DESIGN_WIDTH
  static isIOS = Platform.OS === 'ios'
  static isANDROID = Platform.OS === 'android'
  static statusBarHeight = Platform.OS === 'ios' ? StatusBarManager?.HEIGHT : ExtraDimensions.get('STATUS_BAR_HEIGHT')

  static isFirst = async (action: string = '', isUpdate: boolean = true) => {
    const key = `isFirst_${action}`
    const res = await AsyncStorage.getItem(key)
    if (res) {
      return false
    } else {
      isUpdate && AsyncStorage.setItem(key, 'true')
      return true
    }
  }
}

export default System
